﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// ShoppingCart 的摘要描述
/// </summary>
public class ShoppingCart
{	
  public string ShoppingCartId { get; set; }
  public DateTime Created{get;set;}
  public DateTime LastUpdated{get;set;} 
  //定義購物車中的項目集合
  public List<CartItem> CartItems = new List<CartItem>();

  public void AddToCart(CartItem newCartItem)
  { 
    var cartItem = CartItems.SingleOrDefault(c => c.SMAS_CODE == newCartItem.SMAS_CODE 
      && c.ProdCatID == newCartItem.ProdCatID && c.Prod_Cat_ID == newCartItem.Prod_Cat_ID);    
    //var cartItem = CartItems.SingleOrDefault(c => c.SMAS_CODE == newCartItem.SMAS_CODE);
    if (cartItem == null)
    {
      // 假設購物車商品不存在，則建立一筆購物車新商品      
      this.CartItems.Add(newCartItem);
    }
    else
    {
      //若是購物車此商品已存在，則數量累加
      cartItem.Quantity += newCartItem.Quantity;
      cartItem.Price = newCartItem.Price;
      cartItem.Prod_Cat_ID = newCartItem.Prod_Cat_ID;
      cartItem.Mileage = newCartItem.Mileage;
      cartItem.LastUpdated = DateTime.Now;
    }
  }

  /// <summary>
  /// 取得購物車中的計算總金額 
  /// </summary>
  /// <returns></returns>
  public decimal GetTotal()
  { 
    decimal? total = decimal.Zero;
    total = (decimal?)(from items in this.CartItems
                       select (int?)items.Quantity * items.Price)
                       .Sum();
    return total ?? 0;
  }
  /// <summary>
  /// 計算購物車中所有商品的總數量
  /// </summary>
  /// <returns></returns>
  public int GetCount()
  {        
    int? count = (from cartItems in this.CartItems                  
                  select (int?)cartItems.Quantity).Sum();   
    return count ?? 0;
  }

  /// <summary>
  /// 計算購物車中項目筆數
  /// </summary>
  /// <returns></returns>
  public int GetItemCount()
  {
    int? count = CartItems.Count();
    return count ?? 0;
  }

  /// <summary>
  /// 移除購物車中的某項目
  /// </summary>
  /// <param name="removeSmasCode">SMAS_CODE</param>
  public void RemoveItem(string removeSmasCode)
  {
    try
    {
      var myItem = (from c in this.CartItems
                    where c.SMAS_CODE == removeSmasCode
                    select c).FirstOrDefault();
      if (myItem != null)
      { 
        this.CartItems.Remove(myItem);
      }
    }
    catch (Exception exp)
    {
      throw new Exception("ERROR: 無法移除購物車商品 - " + exp.Message.ToString(), exp);
    }
  }

 /// <summary>
  /// 更新購物車某項目的數量
 /// </summary>
 /// <param name="updateSmasCode">SMAS_CODE</param>
 /// <param name="quantity"></param>
  public void UpdateItem(string updateSmasCode, int quantity)
  {
    try
    {
      var myItem = (from c in this.CartItems
                    where c.SMAS_CODE == updateSmasCode
                    select c).FirstOrDefault();
      if (myItem != null)
      {
        myItem.Quantity = quantity;
        myItem.LastUpdated = DateTime.Now;
      }
    }
    catch (Exception exp)
    {
      throw new Exception("ERROR: 無法更新購物車商品  - " + exp.Message.ToString(), exp);
    }
  }

  /// <summary>
  /// 更新購物車單一項目的狀態與狀態訊息
  /// </summary>
  /// <param name="updateSmasCode">SMAS_CODE</param>
  /// <param name="prodStatus">狀態</param>
  /// <param name="prodMemo">狀態訊息</param>
  public void UpdateItemStatusMemo(string updateSmasCode, string prod_Status, string prod_Memo)
  {
      try
      {
          var myItem = (from c in this.CartItems
                        where c.SMAS_CODE == updateSmasCode
                        select c).FirstOrDefault();
          if (myItem != null)
          {
              myItem.Prod_Status = prod_Status;
              myItem.Prod_Memo = prod_Memo;
          }
      }
      catch (Exception exp)
      {
          throw new Exception("ERROR: 無法更新購物車商品  - " + exp.Message.ToString(), exp);
      }
  }

  /// <summary>
  /// 清空購物車
  /// </summary>
  public void EmptyCart()
  {
    CartItems.Clear();    
  }
}